﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;

namespace LY.DataAccess.Repository
{
    public class LoginReponsitory : BaseRepository
    {
        /// <summary>
        /// 会员登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Member_EX ValidateLogOn(string username, string password)
        {
            string sql = "select ISNULL((SELECT SUM(INCOME)-SUM(OutPay) FROM U_Score where U_Score.MemberId=U_Member.ID),0)AS ScoreStr,* from U_Member where (Name=@Name OR Mobilenumber=@Name) AND Password=@Password";
            return base.Get<Member_EX>(sql, new { Name = username, Password = password });
        }

        //验证用户名是否重
        public int RegisterValite(string name)
        {
            string sql = " SELECT COUNT(ID) FROM U_Member WHERE Name=@Name";
            return base.Get<int>(sql, new {Name=name });
        }
        //验证手机号是否重
        public int MobileValite(string mobile)
        {
            string sql = " SELECT COUNT(ID) FROM U_Member WHERE Mobilenumber=@MOBILE";
            return base.Get<int>(sql, new { MOBILE = mobile });
        }
        //手机验证码是否有效
        public int MobileCodeValite(string mobile, string code, int codeId)
        {

            string sql = " SELECT COUNT(ID) FROM T_Code WHERE Tel=@MOBILE AND Code=@CODE AND Id=@ID";
            //string sql = " SELECT COUNT(ID) FROM T_Code WHERE Tel=@MOBILE  AND Id=@ID";
            return base.Get<int>(sql, new { MOBILE = mobile, Code=code, ID = codeId });
        }
        //验证用户账号和手机号 是否正确 
        public int MemberMobileValite(string name, string mobile)
        {
            string sql = " SELECT COUNT(ID) FROM U_Member WHERE Name=@Name AND Mobilenumber=@MOBILE ";
            return base.Get<int>(sql, new { MOBILE = mobile, Name = name });
        }
        //根据ID获取会员信息
        public Member_EX GetUserById(int id)
        {
            string sql = "select * from U_Member where ID=@ID ";
            return base.Get<Member_EX>(sql, new { ID = id });
        }

        //重置密码
        public int ResetSave(U_Member member)
        {
            string sql = "UPDATE U_Member SET Password=@Password where Mobilenumber=@Mobilenumber AND Name=@Name ";
            return base.Excute(sql, new { Mobilenumber = member.Mobilenumber, Name = member.Name, Password=member.Password });
        }
    }
}
